<template>
  <el-dialog
    class="bind-contract-task-dialog"
    width="500px"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    v-dialogDrag="true"
    @open="handleDialogOpen"
    @closed="handleDialogClosed"
  >
    <template slot="title">
      <span class="el-dialog__title">绑定合同评审任务</span>
    </template>

    <el-form
      ref="bindContractTaskForm"
      :model="formData"
      :rules="formRules"
      size="small"
      label-width="100px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="合同评审任务:" prop="tasksContractId" label-width="120px">
            <el-select v-model="formData.tasksContractId" placeholder="请选择" style="width: 100%" clearable>
              <el-option
                v-for="(item, index) in tasksContractList"
                :key="index"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="dialog-footer" slot="footer">
      <el-button style="margin-right: 50px;" type="primary" @click="handleSave">确认</el-button>
      <el-button @click="dialogVisible = false">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { tasksContractBindList} from '@/api/person/entrustAgreement'
import { bindContractTask } from '@/api/person/contract'

export default {
  name: 'BindContractTaskDialog',
  components: {},
  dicts: [],
  props: {
    show: {
      type: Boolean,
      default: false,
    },
    row: {
      type: Object,
      default: () => ({})
    },
  },
  computed: {
    dialogVisible: {
      get() {
        return this.show
      },
      set(value) {
        this.$emit('update:show', value)
      }
    }
  },
  data() {
    return {
      tasksContractList: [],
      formData: {},
      formRules: {
        tasksContractId: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
      },
    }
  },
  created() {
    this.initFormData()
  },
  methods: {
    // 弹框打开时
    handleDialogOpen() {
      this.getTasksContractList()
    },
    // 弹框关闭时
    handleDialogClosed() {
      this.initFormData()
      this.$refs.bindContractTaskForm.resetFields()
    },
    // 获取合同评审任务列表
    getTasksContractList() {
      const params = { entrustOrganizationId: this.row.entrustOrganizationId }
      tasksContractBindList(params).then(res => {
        this.tasksContractList = res.data || [];
      }).catch(err => {})
    },
    // 初始化表单数据
    initFormData() {
      this.formData = {
        tasksContractId: '',
      }
    },
    // 获取表单数据
    getFormData() {
      const data = JSON.parse(JSON.stringify(this.formData))
      data.agreementId = this.row.agreementId
      return data
    },
    // 点击保存
    handleSave() {
      this.$refs.bindContractTaskForm.validate(valid => {
        if (!valid) return
        const data = this.getFormData()
        bindContractTask(data).then(res => {
          this.$message.success('操作成功')
          this.dialogVisible = false
          this.$emit('refresh')
        }).catch(err => {})
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.bind-contract-task-dialog {

  ::v-deep .el-dialog__body {
    max-height: 85vh;
    overflow-y: auto;
  }
}
</style>
